package com.qf;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.model.Article;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class test {
    @Autowired
    private TransportClient transportClient;
    @Autowired
    private ObjectMapper objectMapper;


    @Test
    public void demo01() throws Exception {
        Article article = new Article();
        article.setTitle("华为手机很棒2");
        article.setContent("华为手机真的很棒2");
        article.setId(2L);

        IndexResponse indexResponse = transportClient.prepareIndex("blog01", "article", "1002")
                .setSource(objectMapper.writeValueAsString(article), XContentType.JSON).get();


        System.out.println(indexResponse);

        GetResponse getResponse = transportClient.prepareGet("blog01", "article", "1001").get();
        System.out.println(getResponse.getSourceAsString());


    }

}
